package excel;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;

public class Main {
    private Map<String, Source> map = new HashMap<>();

    public static void main(String[] args) throws Exception {
        Main m = new Main();
        m.read();
    }

    public void read() throws Exception {

        Workbook workbook = Workbook.getWorkbook(new File("G:/统计.xls"));
        Sheet sheet = workbook.getSheet(1);

        int rows = sheet.getRows();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        Calendar cal = Calendar.getInstance();
       
        for (int i = 1; i < rows - 3; i++) {
            Cell[] row = sheet.getRow(i);
            if (row[1].getContents().startsWith("月卡")) {

                String code = row[0].getContents();
                if (code.indexOf("-") > 0) {
                    int index = code.indexOf("-");
                    code = code.substring(0, index);
                }

                if (map.containsKey(code)) {
                    Source source = map.remove(code);
                    String begin = source.getBegin();
                    String end = source.getEnd();
                    String t = row[3].getContents();
                    
                    Date time = sdf.parse(t);
                    Date endTime = sdf.parse(end);
                    Date start = sdf.parse(begin);
                    if (time.getTime() > endTime.getTime()) {
                        source.setEnd(sdf.format(time));
                        end = sdf.format(time);
                    }
                    if (time.getTime() < start.getTime()) {
                        source.setBegin(sdf.format(time));
                        begin = sdf.format(time);
                    }
                    Source sou = new Source();
                    sou.setCode(code);
                    sou.setBegin(begin);
                    sou.setEnd(end);
                    
                    map.put(code, sou);
                    
                } else {
                    Source sou = new Source();
                    sou.setCode(code);
                    sou.setBegin(row[3].getContents());
                    sou.setEnd(row[3].getContents());

                    map.put(code, sou);
                }

                // System.out.println(sou.getCode() + ":" + sou.getBegin() + ":" + sou.getEnd());
            }
        }
        printMap(map);

    }

    public void write() {

    }

    public void printMap(Map<String, Source> map) {
        Set<Entry<String, Source>> entrySet = map.entrySet();
        for (Entry<String, Source> entry : entrySet) {
            Source value = entry.getValue();

            System.out.println(entry.getKey() + "\t" + value.getCode() + "\t" + value.getBegin() + "\t"
                    + value.getEnd());
        }
    }
}
